
#define _gam_nvault_included 1


#if !defined _gam_globals_included
	#include <gam/stock>
#endif

#include <nvault>


new gVault;
new const KEY_GLOBAL[] = "GAM-G-%s";
new const DATA_GLOBAL[] = "%d#%d";
new const KEY_WPN[]="GAM-WPN-%d-%s";
new const DATA_WPN[] ="%d#%d#%d#%d#%d#%d#%d#%d#%d";
new const KEY_SETTINGS[]="GAM-S-%s";
new const DATA_SETTINGS[] = "%d#%d#%d#%d#%f.2#%f.2";

stock initSaving(){
	gVault = nvault_open("GAM");
}

stock savePlayerWpnStats(id){
	if(is_user_bot(id)) return;
	
	new key[MAX_KEY_LEN + 12];
	new data[128];
	new saveKey[MAX_KEY_LEN];
	copy(saveKey, MAX_KEY_LEN-1, gPlayerKey[id]);
	for(new wpn=1;wpn<WEAPONS;wpn++) {
		if(isLevelWeapon(wpn)) {
			formatex(key, charsmax(key), KEY_WPN, wpn, saveKey);
			formatex(data,charsmax(data), DATA_WPN, 
			gPlayerLevelInfo[id][wpn],
			gPlayerInfo[id][wpn][REQ_KILLS_TOTAL],
			gPlayerInfo[id][wpn][REQ_HS_TOTAL],
			gPlayerLevelReq[id][wpn][REQ_KILLS_ROUND],
			gPlayerLevelReq[id][wpn][REQ_KILLS_MAP],
			gPlayerLevelReq[id][wpn][REQ_KILLS_TOTAL],
			gPlayerLevelReq[id][wpn][REQ_HS_ROUND],
			gPlayerLevelReq[id][wpn][REQ_HS_MAP],
			gPlayerLevelReq[id][wpn][REQ_HS_TOTAL]);
			nvault_set(gVault, key, data);
		}
	}
	formatex(key, charsmax(key), KEY_GLOBAL, saveKey);
	formatex(data, charsmax(data), DATA_GLOBAL, gPlayerXp[id][T_TOTAL],  gPlayerRank[id]);
	nvault_set(gVault, key, data);
}

stock readPlayerWpnStats(id){
	if(is_user_bot(id)) return;

	new key[MAX_KEY_LEN + 12];
	new data[128];
	new saveKey[MAX_KEY_LEN];
	copy(saveKey, MAX_KEY_LEN-1, gPlayerKey[id]);
	new timestamp;
	new sLevel[3];
	new sKills[10];
	new sHs[10];
	new sReq[LEVEL_REQ][2];
	
	for(new wpn=1;wpn<WEAPONS;wpn++){
		if(isLevelWeapon(wpn)) {
			formatex(key, charsmax(key), KEY_WPN, wpn, saveKey);
			if(nvault_lookup(gVault, key, data, charsmax(data), timestamp)){
				replace_all(data, charsmax(data), "#", " ");
				parse(data, sLevel, 1, sKills, 9,sHs , 9,
				sReq[REQ_KILLS_ROUND], 1,
				sReq[REQ_KILLS_MAP], 1,
				sReq[REQ_KILLS_TOTAL], 1,
				sReq[REQ_HS_ROUND], 1,
				sReq[REQ_HS_MAP], 1,
				sReq[REQ_HS_TOTAL], 1);
				
				gPlayerLevelInfo[id][wpn] = str_to_num(sLevel);
				gPlayerInfo[id][wpn][REQ_KILLS_TOTAL] = str_to_num(sKills);
				gPlayerInfo[id][wpn][REQ_HS_TOTAL] = str_to_num(sHs);
				gPlayerLevelReq[id][wpn][REQ_KILLS_ROUND] = str_to_num(sReq[REQ_KILLS_ROUND]);
				gPlayerLevelReq[id][wpn][REQ_KILLS_MAP] = str_to_num(sReq[REQ_KILLS_MAP]);
				gPlayerLevelReq[id][wpn][REQ_KILLS_TOTAL] = str_to_num(sReq[REQ_KILLS_TOTAL]);
				gPlayerLevelReq[id][wpn][REQ_HS_ROUND] = str_to_num(sReq[REQ_HS_ROUND]);
				gPlayerLevelReq[id][wpn][REQ_HS_MAP] = str_to_num(sReq[REQ_HS_MAP]);
				gPlayerLevelReq[id][wpn][REQ_HS_TOTAL] = str_to_num(sReq[REQ_HS_TOTAL]);
			} else {
				gPlayerLevelInfo[id][wpn] = 0;
				gPlayerInfo[id][wpn][REQ_KILLS_TOTAL] = 0;
				gPlayerInfo[id][wpn][REQ_HS_TOTAL] = 0;
				gPlayerLevelReq[id][wpn][REQ_KILLS_ROUND] = 0;
				gPlayerLevelReq[id][wpn][REQ_KILLS_MAP] = 0;
				gPlayerLevelReq[id][wpn][REQ_KILLS_TOTAL] = 0;
				gPlayerLevelReq[id][wpn][REQ_HS_ROUND] = 0;
				gPlayerLevelReq[id][wpn][REQ_HS_MAP] = 0;
				gPlayerLevelReq[id][wpn][REQ_HS_TOTAL] = 0;
			}
		}
	}
	formatex(key, charsmax(key), KEY_GLOBAL, saveKey);
	if(nvault_lookup(gVault, key, data,charsmax(data), timestamp)){
		replace_all(data, charsmax(data), "#", " ");
		parse(data, sKills, 9, sLevel, 2);
		gPlayerXp[id][T_TOTAL] = str_to_num(sKills);
		gPlayerRank[id] = str_to_num(sLevel);
	} else {
		gPlayerXp[id][T_TOTAL] = 0;
		gPlayerRank[id] = 0;
	}
}


stock readPlayerSettings(id){
	if(is_user_bot(id)) return;

	new key[MAX_KEY_LEN + 7];
	new data[24];
	new timestamp;
	formatex(key,charsmax(key), KEY_SETTINGS,  gPlayerKey[id]);
	if(nvault_lookup(gVault, key, data, charsmax(data), timestamp)){
		new str[SETTINGS][6];
		replace_all(data, charsmax(data), "#", " ");
		
		parse(data, str[SETTINGS_DISP_RANK], 1,
			str[SETTINGS_DISP_WPN_LVL], 1,
			str[SETTINGS_SHOW_LVL_ON_KILL], 1,
			str[SETTINGS_SHOW_LVL_ON_WPN_CHANGE], 1,
			str[SETTINGS_DISP_X], 5,
			str[SETTINGS_DISP_Y], 5);
		
		gPlayerSettings[id][SETTINGS_DISP_RANK] = str_to_num(str[SETTINGS_DISP_RANK]);
		gPlayerSettings[id][SETTINGS_DISP_WPN_LVL] = str_to_num(str[SETTINGS_DISP_WPN_LVL]);
		gPlayerSettings[id][SETTINGS_SHOW_LVL_ON_KILL] = str_to_num(str[SETTINGS_SHOW_LVL_ON_KILL]);
		gPlayerSettings[id][SETTINGS_SHOW_LVL_ON_WPN_CHANGE] = str_to_num(str[SETTINGS_SHOW_LVL_ON_WPN_CHANGE]);
		gPlayerSettings[id][SETTINGS_DISP_X] = _:str_to_float(str[SETTINGS_DISP_X]);
		gPlayerSettings[id][SETTINGS_DISP_Y] = _:str_to_float(str[SETTINGS_DISP_Y]);
	} else {
		defaultPlayerSettings(id);
	}
}

stock savePlayerSettings(id){
	if(is_user_bot(id)) return;

	new key[MAX_KEY_LEN + 7];
	new data[24];
	formatex(key, charsmax(key), KEY_SETTINGS,  gPlayerKey[id]);
	formatex(data, charsmax(data),  DATA_SETTINGS,
			gPlayerSettings[id][SETTINGS_DISP_RANK],
			gPlayerSettings[id][SETTINGS_DISP_WPN_LVL],
			gPlayerSettings[id][SETTINGS_SHOW_LVL_ON_KILL],
			gPlayerSettings[id][SETTINGS_SHOW_LVL_ON_WPN_CHANGE],
			gPlayerSettings[id][SETTINGS_DISP_X],
			gPlayerSettings[id][SETTINGS_DISP_Y]);
	nvault_set(gVault, key, data);
}
